package org.example.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.dto.CurriculumDto;
import org.example.dto.GiveInstructionDto;
import org.example.service.CurriculumService;
import org.example.service.GiveInstructionService;
import org.example.service.TestService;
import org.example.utils.JsonUtils;
import org.example.vo.GiveInstructionVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Rongchang Wu
 * @version 1.0
 * @description: TODO
 */
@Controller
public class GiveInstructionController {
    @Resource
    GiveInstructionService service;
    @Resource
    CurriculumService curriculumService;

    @GetMapping("/getGiveInstruction")
    @ResponseBody
    public void getGiveInstruction(HttpServletResponse resp, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<GiveInstructionDto> giveInstructionList = service.queryAllGiveInstruction();
        Integer count = service.countCurriculum();
        //以上调用service获取授课信息和数量，返回给giveInstructionList和count

        JsonUtils.customJson(resp, giveInstructionList, count);
    }

    @GetMapping("/getCurriculumInGiveInstruction")
    @ResponseBody
    public List<CurriculumDto> getCurriculum() {
        List<CurriculumDto> curriculumList = curriculumService.queryAllCurriculum();
        //以上调用service获取全部课程信息

        return curriculumList;
    }

    @GetMapping(value = "/deleteGiveInstruction", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String deleteGiveInstruction(String curriculumId) {
        String[] curriculumIds = curriculumId.split(",");
        Integer status = 0;
        for (int i = 0; i < curriculumIds.length; i++) {
            status += service.deleteGiveInstruction(Integer.parseInt(curriculumIds[i]));
        }
        //以上service根据id删除课程，返回结果给status

        return status > 0 ? "success" : "error";
    }

    @GetMapping(value = "/addGiveInstruction", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String addGiveInstruction(GiveInstructionVo giveInstruction) {
        Integer status = service.addGiveInstruction(giveInstruction);
        //以上调用service添加授课关系

        return status > 0 ? "success" : "error";
    }

    @GetMapping("/getGiveInstructionByCurriculumId")
    @ResponseBody
    public GiveInstructionDto getGiveInstructionByCurriculumId(Integer curriculumId) {
        GiveInstructionDto giveInstruction = service.queryGiveInstructionByCurriculumId(curriculumId);
        //以上调用service根据课程号获取对应课程信息

        return giveInstruction;
    }

    @GetMapping(value = "/editGiveInstruction", produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String editGiveInstruction(GiveInstructionVo giveInstruction) {
        Integer status = service.updateGiveInstruction(giveInstruction);
        //以上调用service更新课程信息

        return status > 0 ? "success" : "error";
    }

    @GetMapping("/getGiveInstructionByTeacherName")
    @ResponseBody
    public void getGiveInstructionByTeacherName(HttpServletResponse resp, String teacherName, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<GiveInstructionDto> giveInstructionList = service.queryGiveInstructionByTeacher(teacherName);
        Integer count = service.countGiveInstructionByTeacherName(teacherName);
        //以上调用service根据教师名获取对应的授课信息和该教师授课数量

        JsonUtils.customJson(resp, giveInstructionList, count);
    }
}
